package com.sdp.right.db.sys_flow_instance.service.impl;

import java.time.LocalDateTime;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.core.bizc.threadLocal.ContextManager;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.right.db.sys_flow_instance.mapper.SysFlowInstanceMapper;
import com.sdp.right.db.sys_flow_instance.service.ISysFlowInstanceService;
import com.sdp.right.entity.SysFlowInstance;
import com.sdp.right.entity.SysFlowNodeAttributeVersion;
import com.sdp.right.flow.dto.FlowParam;
import com.sdp.right.service.RemoveSystemService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-07-17
 */
@Service
public class SysFlowInstanceServiceImpl extends ServiceImpl<SysFlowInstanceMapper, SysFlowInstance> implements ISysFlowInstanceService,RemoveSystemService {

	@Override
	public SysFlowInstance createFlowInstance(FlowParam flowParam) {
		SysFlowNodeAttributeVersion attribute = flowParam.getCurrentNodeAttribute();
		SysFlowInstance instance = new SysFlowInstance();
		instance.setTemplateName(flowParam.getTemplateName());
		instance.setTemplateNo(flowParam.getTemplateNo());
		instance.setTemplateVersion(attribute.getTemplateVersion());
		instance.setSystemNo(attribute.getSystemNo());
		instance.setStartEmpName(ContextManager.getContextData(CoreConstantsEnum.token中姓名的属性名.getCode()));
		instance.setStartEmpNo(ContextManager.getContextData(CoreConstantsEnum.token中工号的属性名.getCode()));
		instance.setStartTime(LocalDateTime.now());
		save(instance);
		return instance;
	}

	@Override
	public void clearHisFlowInstanceVersion(String version, String templateNo) {
		LambdaQueryWrapper<SysFlowInstance> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.ne(SysFlowInstance::getTemplateVersion, version);
		queryWrapper.eq(SysFlowInstance::getTemplateNo, templateNo);
		remove(queryWrapper);
		
	}

	@Override
	public void removeBySystemNo(String systemNo) {
		LambdaQueryWrapper<SysFlowInstance> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowInstance::getSystemNo, systemNo);
		remove(queryWrapper);
		
	}

}
